#include "OpenCLWrapper.hpp"
#include <dlfcn.h>
#include <memory>
#include <string>
#include <vector>
#include <mutex>

bool OpenCLSymbols::LoadOpenCLLibrary() {
    if (handle_ != nullptr) {
        return true;
    }
    static const std::vector<std::string> gOpencl_library_paths = {
            // linux
            "/usr/lib/libOpenCL.so",
            "/usr/local/lib/libOpenCL.so",
            "/usr/local/lib/libpocl.so",
            "/usr/lib64/libOpenCL.so",
            "/usr/lib32/libOpenCL.so",
            "/usr/lib/x86_64-linux-gnu/libOpenCL.so",
            "libOpenCL.so"
            // android
            "/vendor/lib64/libOpenCL.so",
            "/system/vendor/lib64/libOpenCL.so",
    };

    for (const auto &opencl_lib : gOpencl_library_paths) {
        if (LoadLibraryFromPath(opencl_lib)) {
            LOGI("libpaths hit!\n");
            return true;
        }
    }
    LOGI("libpaths not hit!\n");
    return false;
}

bool OpenCLSymbols::UnLoadOpenCLLibrary() {
    if (handle_ != nullptr) {
        if (dlclose(handle_) != 0) {
            return false;
        }
        handle_ = nullptr;
        return true;
    }
    return true;
}

bool OpenCLSymbols::isError() {
    return mIsError;
}

bool OpenCLSymbols::isSvmError() {
    return mSvmError;
}

bool OpenCLSymbols::isPropError() {
    return mPropError;
}

bool OpenCLSymbols::isQcomError() {
    return mQcomError;
}

bool OpenCLSymbols::isSupportAhardwareBufferFunc(){
    return mIsSupportAhardwareBuffer;
}

bool OpenCLSymbols::getFuncAddress(cl_platform_id platform, const char *func_name){
    if(clGetExtensionFunctionAddressForPlatform != nullptr){
        clImportMemoryARM = reinterpret_cast<clImportMemoryARMFunc>(clGetExtensionFunctionAddressForPlatform(platform, "clImportMemoryARM"));
        if(clImportMemoryARM == nullptr){
            return false;
        }
    }else if(clGetExtensionFunctionAddress != nullptr){
        clImportMemoryARM = reinterpret_cast<clImportMemoryARMFunc>(clGetExtensionFunctionAddress("clImportMemoryARM"));
        if(clImportMemoryARM == nullptr){
            return false;
        }
    } else{
        return false;
    }
    return true;
}

bool OpenCLSymbols::LoadLibraryFromPath(const std::string &library_path) {
    handle_ = dlopen(library_path.c_str(), RTLD_NOW | RTLD_LOCAL);
    if (handle_ == nullptr) {
        return false;
    }

    typedef void* (*loadOpenCLPointerFunc)(const char* name);
    typedef void (*enableOpenCLFunc)();
    loadOpenCLPointerFunc loadOpenCLPointer = nullptr;
    enableOpenCLFunc enableOpenCL = reinterpret_cast<enableOpenCLFunc>(dlsym(handle_, "enableOpenCL"));
    if(enableOpenCL != nullptr){
        enableOpenCL();
        loadOpenCLPointer = reinterpret_cast<loadOpenCLPointerFunc>(dlsym(handle_, "loadOpenCLPointer"));
    }
#define WRP_LOAD_FUNCTION_PTR(func_name) func_name = reinterpret_cast<func_name##Func>(dlsym(handle_, #func_name)); \
    if(func_name == nullptr && loadOpenCLPointer != nullptr){ \
        func_name = reinterpret_cast<func_name##Func>(loadOpenCLPointer(#func_name)); \
    } \
    if(func_name == nullptr){ \
        mIsError = true; \
    }

#define WRP_LOAD_SVM_PTR(func_name) func_name = reinterpret_cast<func_name##Func>(dlsym(handle_, #func_name)); \
    if(func_name == nullptr && loadOpenCLPointer != nullptr){ \
        func_name = reinterpret_cast<func_name##Func>(loadOpenCLPointer(#func_name)); \
    } \
    if(func_name == nullptr){ \
        mSvmError = true; \
    }

#define WRP_LOAD_PROP_PTR(func_name) func_name = reinterpret_cast<func_name##Func>(dlsym(handle_, #func_name)); \
    if(func_name == nullptr && loadOpenCLPointer != nullptr){ \
        func_name = reinterpret_cast<func_name##Func>(loadOpenCLPointer(#func_name)); \
    } \
    if(func_name == nullptr){ \
        mPropError = true; \
    }

    WRP_LOAD_FUNCTION_PTR(clGetPlatformIDs);
    WRP_LOAD_FUNCTION_PTR(clGetPlatformInfo);
    WRP_LOAD_FUNCTION_PTR(clBuildProgram);
    WRP_LOAD_FUNCTION_PTR(clEnqueueNDRangeKernel);
    WRP_LOAD_FUNCTION_PTR(clSetKernelArg);
    WRP_LOAD_FUNCTION_PTR(clReleaseKernel);
    WRP_LOAD_FUNCTION_PTR(clCreateProgramWithSource);
    WRP_LOAD_FUNCTION_PTR(clCreateBuffer);
    WRP_LOAD_FUNCTION_PTR(clCreateImage2D);
    WRP_LOAD_FUNCTION_PTR(clRetainKernel);
    WRP_LOAD_FUNCTION_PTR(clCreateKernel);
    WRP_LOAD_FUNCTION_PTR(clGetProgramInfo);
    WRP_LOAD_FUNCTION_PTR(clFlush);
    WRP_LOAD_FUNCTION_PTR(clFinish);
    WRP_LOAD_FUNCTION_PTR(clReleaseProgram);
    WRP_LOAD_FUNCTION_PTR(clRetainContext);
    WRP_LOAD_FUNCTION_PTR(clGetContextInfo);
    WRP_LOAD_FUNCTION_PTR(clCreateProgramWithBinary);
    WRP_LOAD_FUNCTION_PTR(clCreateCommandQueue);
    WRP_LOAD_FUNCTION_PTR(clReleaseCommandQueue);
    WRP_LOAD_FUNCTION_PTR(clEnqueueMapBuffer);
    WRP_LOAD_FUNCTION_PTR(clEnqueueMapImage);
    WRP_LOAD_FUNCTION_PTR(clRetainProgram);
    WRP_LOAD_FUNCTION_PTR(clGetProgramBuildInfo);
    WRP_LOAD_FUNCTION_PTR(clEnqueueReadBuffer);
    WRP_LOAD_FUNCTION_PTR(clEnqueueWriteBuffer);
    WRP_LOAD_FUNCTION_PTR(clEnqueueCopyBuffer);
    WRP_LOAD_FUNCTION_PTR(clWaitForEvents);
    WRP_LOAD_FUNCTION_PTR(clReleaseEvent);
    WRP_LOAD_FUNCTION_PTR(clCreateContext);
    WRP_LOAD_FUNCTION_PTR(clCreateContextFromType);
    WRP_LOAD_FUNCTION_PTR(clReleaseContext);
    WRP_LOAD_FUNCTION_PTR(clRetainCommandQueue);
    WRP_LOAD_FUNCTION_PTR(clEnqueueUnmapMemObject);
    WRP_LOAD_FUNCTION_PTR(clRetainMemObject);
    WRP_LOAD_FUNCTION_PTR(clReleaseMemObject);
    WRP_LOAD_FUNCTION_PTR(clGetDeviceInfo);
    WRP_LOAD_FUNCTION_PTR(clGetDeviceIDs);
    WRP_LOAD_FUNCTION_PTR(clRetainEvent);
    WRP_LOAD_FUNCTION_PTR(clGetKernelWorkGroupInfo);
    WRP_LOAD_FUNCTION_PTR(clGetEventInfo);
    WRP_LOAD_FUNCTION_PTR(clGetEventProfilingInfo);
    WRP_LOAD_FUNCTION_PTR(clGetMemObjectInfo);
    WRP_LOAD_FUNCTION_PTR(clGetImageInfo);
    WRP_LOAD_FUNCTION_PTR(clEnqueueCopyImage);
    WRP_LOAD_FUNCTION_PTR(clEnqueueReadImage);
    WRP_LOAD_FUNCTION_PTR(clEnqueueWriteImage);
    WRP_LOAD_FUNCTION_PTR(clGetExtensionFunctionAddress);
    WRP_LOAD_FUNCTION_PTR(clGetExtensionFunctionAddressForPlatform);

    WRP_LOAD_PROP_PTR(clCreateCommandQueueWithProperties);
    WRP_LOAD_SVM_PTR(clSVMAlloc);
    WRP_LOAD_SVM_PTR(clSVMFree);
    WRP_LOAD_SVM_PTR(clEnqueueSVMMap);
    WRP_LOAD_SVM_PTR(clEnqueueSVMUnmap);
    WRP_LOAD_SVM_PTR(clSetKernelArgSVMPointer);
#undef WRP_LOAD_FUNCTION_PTR

    return true;
}

OpenCLSymbolsOperator* OpenCLSymbolsOperator::createOpenCLSymbolsOperatorSingleInstance() {
    static std::once_flag sFlagInitSymbols;
    static OpenCLSymbolsOperator* gInstance = nullptr;
    std::call_once(sFlagInitSymbols, [&]() {
        gInstance = new OpenCLSymbolsOperator;
    });
    return gInstance;
}
std::shared_ptr<OpenCLSymbols> OpenCLSymbolsOperator::gOpenclSymbols;

OpenCLSymbols *OpenCLSymbolsOperator::getOpenclSymbolsPtr() {
    return gOpenclSymbols.get();
}

OpenCLSymbolsOperator::OpenCLSymbolsOperator() {
    LOGI("====start OpenCLSymbolsOperator !====\n");
    if (gOpenclSymbols.get() == nullptr) {
        gOpenclSymbols.reset(new OpenCLSymbols());
    } else {
        LOGI(" OpenCLSymbols already now !\n");
    }

    if (false == gOpenclSymbols->LoadOpenCLLibrary()) {
        gOpenclSymbols.reset();
    }
    LOGI("====end OpenCLSymbolsOperator !====\n\n");
}

OpenCLSymbolsOperator::~OpenCLSymbolsOperator() {
    LOGI("start ~OpenCLSymbolsOperator !\n");
    if (nullptr == gOpenclSymbols.get()) {
        return;
    }
    gOpenclSymbols.get()->UnLoadOpenCLLibrary();
    LOGI("end ~OpenCLSymbolsOperator !\n");
}

cl_int CL_API_CALL clGetPlatformIDs(cl_uint num_entries, cl_platform_id *platforms, cl_uint *num_platforms) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clGetPlatformIDs;
WRP_CHECK_NOTNULL(func);
return func(num_entries, platforms, num_platforms);
}

cl_int CL_API_CALL clGetPlatformInfo(cl_platform_id platform, cl_platform_info param_name, size_t param_value_size,
void *param_value, size_t *param_value_size_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clGetPlatformInfo;
WRP_CHECK_NOTNULL(func);
return func(platform, param_name, param_value_size, param_value, param_value_size_ret);
}

cl_int CL_API_CALL clGetDeviceIDs(cl_platform_id platform, cl_device_type device_type, cl_uint num_entries, cl_device_id *devices,
        cl_uint *num_devices) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clGetDeviceIDs;
WRP_CHECK_NOTNULL(func);
return func(platform, device_type, num_entries, devices, num_devices);
}

cl_int CL_API_CALL clGetDeviceInfo(cl_device_id device, cl_device_info param_name, size_t param_value_size, void *param_value,
        size_t *param_value_size_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clGetDeviceInfo;
WRP_CHECK_NOTNULL(func);
return func(device, param_name, param_value_size, param_value, param_value_size_ret);
}

cl_context CL_API_CALL clCreateContext(const cl_context_properties *properties, cl_uint num_devices, const cl_device_id *devices,
                                       void(CL_CALLBACK *pfn_notify)(const char *, const void *, size_t, void *), void *user_data,
        cl_int *errcode_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clCreateContext;
WRP_CHECK_NOTNULL(func);
return func(properties, num_devices, devices, pfn_notify, user_data, errcode_ret);
}

cl_context CL_API_CALL clCreateContextFromType(const cl_context_properties *properties, cl_device_type device_type,
                                               void(CL_CALLBACK *pfn_notify)(const char *, const void *, size_t, void *),
void *user_data, cl_int *errcode_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clCreateContextFromType;
WRP_CHECK_NOTNULL(func);
return func(properties, device_type, pfn_notify, user_data, errcode_ret);
}

cl_int CL_API_CALL clRetainContext(cl_context context) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clRetainContext;
WRP_CHECK_NOTNULL(func);
return func(context);
}

cl_int CL_API_CALL clReleaseContext(cl_context context) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clReleaseContext;
WRP_CHECK_NOTNULL(func);
return func(context);
}

cl_int CL_API_CALL clGetContextInfo(cl_context context, cl_context_info param_name, size_t param_value_size, void *param_value,
        size_t *param_value_size_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clGetContextInfo;
WRP_CHECK_NOTNULL(func);
return func(context, param_name, param_value_size, param_value, param_value_size_ret);
}

cl_program CL_API_CALL clCreateProgramWithSource(cl_context context, cl_uint count, const char **strings, const size_t *lengths,
        cl_int *errcode_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clCreateProgramWithSource;
WRP_CHECK_NOTNULL(func);
return func(context, count, strings, lengths, errcode_ret);
}

cl_program CL_API_CALL clCreateProgramWithBinary(cl_context context, cl_uint count, const cl_device_id *           device_list, const size_t* length, const unsigned char ** buffer, cl_int * binary_status, cl_int * errcode_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clCreateProgramWithBinary;
WRP_CHECK_NOTNULL(func);
return func(context, count, device_list, length, buffer, binary_status, errcode_ret);
}

cl_int CL_API_CALL clGetProgramInfo(cl_program program, cl_program_info param_name, size_t param_value_size, void *param_value,
        size_t *param_value_size_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clGetProgramInfo;
WRP_CHECK_NOTNULL(func);
return func(program, param_name, param_value_size, param_value, param_value_size_ret);
}

cl_int CL_API_CALL clGetProgramBuildInfo(cl_program program, cl_device_id device, cl_program_build_info param_name,
size_t param_value_size, void *param_value, size_t *param_value_size_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clGetProgramBuildInfo;
WRP_CHECK_NOTNULL(func);
return func(program, device, param_name, param_value_size, param_value, param_value_size_ret);
}

cl_int CL_API_CALL clRetainProgram(cl_program program) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clRetainProgram;
WRP_CHECK_NOTNULL(func);
return func(program);
}

cl_int CL_API_CALL clReleaseProgram(cl_program program) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clReleaseProgram;
WRP_CHECK_NOTNULL(func);
return func(program);
}

cl_int CL_API_CALL clBuildProgram(cl_program program, cl_uint num_devices, const cl_device_id *device_list, const char *options,
void(CL_CALLBACK *pfn_notify)(cl_program program, void *user_data), void *user_data) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clBuildProgram;
WRP_CHECK_NOTNULL(func);
return func(program, num_devices, device_list, options, pfn_notify, user_data);
}

cl_kernel CL_API_CALL clCreateKernel(cl_program program, const char *kernelName, cl_int *errcode_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clCreateKernel;
WRP_CHECK_NOTNULL(func);
return func(program, kernelName, errcode_ret);
}

cl_int CL_API_CALL clRetainKernel(cl_kernel kernel) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clRetainKernel;
WRP_CHECK_NOTNULL(func);
return func(kernel);
}

cl_int CL_API_CALL clReleaseKernel(cl_kernel kernel) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clReleaseKernel;
WRP_CHECK_NOTNULL(func);
return func(kernel);
}

cl_int CL_API_CALL clSetKernelArg(cl_kernel kernel, cl_uint arg_index, size_t arg_size, const void *arg_value) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clSetKernelArg;
WRP_CHECK_NOTNULL(func);
return func(kernel, arg_index, arg_size, arg_value);
}

cl_mem CL_API_CALL clCreateBuffer(cl_context context, cl_mem_flags flags, size_t size, void *host_ptr, cl_int *errcode_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clCreateBuffer;
WRP_CHECK_NOTNULL(func);
return func(context, flags, size, host_ptr, errcode_ret);
}

cl_int CL_API_CALL clRetainMemObject(cl_mem memobj) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clRetainMemObject;
WRP_CHECK_NOTNULL(func);
return func(memobj);
}

cl_int CL_API_CALL clReleaseMemObject(cl_mem memobj) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clReleaseMemObject;
WRP_CHECK_NOTNULL(func);
return func(memobj);
}

cl_int CL_API_CALL clGetImageInfo(cl_mem image, cl_image_info param_name, size_t param_value_size, void *param_value,
        size_t *param_value_size_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clGetImageInfo;
WRP_CHECK_NOTNULL(func);
return func(image, param_name, param_value_size, param_value, param_value_size_ret);
}

cl_int CL_API_CALL clRetainCommandQueue(cl_command_queue command_queue) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clRetainCommandQueue;
WRP_CHECK_NOTNULL(func);
return func(command_queue);
}

cl_int CL_API_CALL clReleaseCommandQueue(cl_command_queue command_queue) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clReleaseCommandQueue;
WRP_CHECK_NOTNULL(func);
return func(command_queue);
}

cl_int CL_API_CALL clEnqueueReadBuffer(cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_read, size_t offset,
        size_t size, void *ptr, cl_uint num_events_in_wait_list, const cl_event *event_wait_list,
        cl_event *event) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clEnqueueReadBuffer;
WRP_CHECK_NOTNULL(func);
return func(command_queue, buffer, blocking_read, offset, size, ptr, num_events_in_wait_list, event_wait_list,
        event);
}

cl_int CL_API_CALL
clEnqueueCopyBuffer(cl_command_queue    command_queue,
cl_mem              src_buffer,
        cl_mem              dst_buffer,
size_t              src_offset,
        size_t              dst_offset,
size_t              size,
        cl_uint             num_events_in_wait_list,
const cl_event *    event_wait_list,
        cl_event *          event) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clEnqueueCopyBuffer;
WRP_CHECK_NOTNULL(func);
return func(command_queue, src_buffer, dst_buffer, src_offset, dst_offset, size, num_events_in_wait_list, event_wait_list, event);
}

cl_int CL_API_CALL clEnqueueWriteBuffer(cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_write, size_t offset,
        size_t size, const void *ptr, cl_uint num_events_in_wait_list,
const cl_event *event_wait_list, cl_event *event) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clEnqueueWriteBuffer;
WRP_CHECK_NOTNULL(func);
return func(command_queue, buffer, blocking_write, offset, size, ptr, num_events_in_wait_list, event_wait_list,
        event);
}

cl_int CL_API_CALL clEnqueueReadImage(cl_command_queue command_queue, cl_mem cl_image, cl_bool is_block, const size_t * origin, const size_t * region, size_t row_pitch,
size_t slice_pitch, void * ptr, cl_uint num_events_in_wait_list, const cl_event * event_wait_list, cl_event * event) CL_API_SUFFIX__VERSION_1_0 {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clEnqueueReadImage;
WRP_CHECK_NOTNULL(func);
return func(command_queue, cl_image, is_block, origin, region, row_pitch, slice_pitch, ptr, num_events_in_wait_list, event_wait_list,
        event);
}

cl_int CL_API_CALL clEnqueueWriteImage(cl_command_queue command_queue, cl_mem cl_image, cl_bool is_block, const size_t * origin, const size_t * region, size_t row_pitch,
size_t slice_pitch, const void * ptr, cl_uint num_events_in_wait_list, const cl_event * event_wait_list, cl_event * event) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clEnqueueWriteImage;
WRP_CHECK_NOTNULL(func);
return func(command_queue, cl_image, is_block, origin, region, row_pitch, slice_pitch, ptr, num_events_in_wait_list, event_wait_list,
        event);
}

void* CL_API_CALL clEnqueueMapBuffer(cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_map, cl_map_flags map_flags,
        size_t offset, size_t size, cl_uint num_events_in_wait_list, const cl_event *event_wait_list,
        cl_event *event, cl_int *errcode_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clEnqueueMapBuffer;
WRP_CHECK_NOTNULL(func);
return func(command_queue, buffer, blocking_map, map_flags, offset, size, num_events_in_wait_list, event_wait_list,
        event, errcode_ret);
}

void* CL_API_CALL clEnqueueMapImage(cl_command_queue command_queue, cl_mem image, cl_bool blocking_map, cl_map_flags map_flags,
const size_t *origin, const size_t *region, size_t *image_row_pitch, size_t *image_slice_pitch,
        cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event,
cl_int *errcode_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clEnqueueMapImage;
WRP_CHECK_NOTNULL(func);
return func(command_queue, image, blocking_map, map_flags, origin, region, image_row_pitch, image_slice_pitch,
        num_events_in_wait_list, event_wait_list, event, errcode_ret);
}

cl_int CL_API_CALL clEnqueueUnmapMemObject(cl_command_queue command_queue, cl_mem memobj, void *mapped_ptr,
        cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clEnqueueUnmapMemObject;
WRP_CHECK_NOTNULL(func);
return func(command_queue, memobj, mapped_ptr, num_events_in_wait_list, event_wait_list, event);
}

cl_int CL_API_CALL clGetKernelWorkGroupInfo(cl_kernel kernel, cl_device_id device, cl_kernel_work_group_info param_name,
size_t param_value_size, void *param_value, size_t *param_value_size_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clGetKernelWorkGroupInfo;
WRP_CHECK_NOTNULL(func);
return func(kernel, device, param_name, param_value_size, param_value, param_value_size_ret);
}

cl_int CL_API_CALL clGetEventProfilingInfo(cl_event event, cl_profiling_info param_name, size_t param_value_size, void *param_value,
        size_t *param_value_size_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clGetEventProfilingInfo;
WRP_CHECK_NOTNULL(func);
return func(event, param_name, param_value_size, param_value, param_value_size_ret);
}

cl_int CL_API_CALL clGetMemObjectInfo(cl_mem memobj, cl_mem_info param_name, size_t param_value_size, void *param_value,
        size_t *param_value_size_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clGetMemObjectInfo;
WRP_CHECK_NOTNULL(func);
return func(memobj, param_name, param_value_size, param_value, param_value_size_ret);
}
cl_int CL_API_CALL clEnqueueNDRangeKernel(cl_command_queue command_queue, cl_kernel kernel, cl_uint work_dim,
const size_t *global_work_offset, const size_t *global_work_size,
const size_t *local_work_size, cl_uint num_events_in_wait_list,
const cl_event *event_wait_list, cl_event *event) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clEnqueueNDRangeKernel;
WRP_CHECK_NOTNULL(func);
return func(command_queue, kernel, work_dim, global_work_offset, global_work_size, local_work_size,
        num_events_in_wait_list, event_wait_list, event);
}

cl_int CL_API_CALL clWaitForEvents(cl_uint num_events, const cl_event *event_list) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clWaitForEvents;
WRP_CHECK_NOTNULL(func);
return func(num_events, event_list);
}

cl_int CL_API_CALL clRetainEvent(cl_event event) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clRetainEvent;
WRP_CHECK_NOTNULL(func);
return func(event);
}

cl_int CL_API_CALL clReleaseEvent(cl_event event) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clReleaseEvent;
WRP_CHECK_NOTNULL(func);
return func(event);
}

cl_int CL_API_CALL clGetEventInfo(cl_event event, cl_event_info param_name, size_t param_value_size, void *param_value,
        size_t *param_value_size_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clGetEventInfo;
WRP_CHECK_NOTNULL(func);
return func(event, param_name, param_value_size, param_value, param_value_size_ret);
}

cl_int CL_API_CALL clFlush(cl_command_queue command_queue) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clFlush;
WRP_CHECK_NOTNULL(func);
return func(command_queue);
}

cl_int CL_API_CALL clFinish(cl_command_queue command_queue) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clFinish;
WRP_CHECK_NOTNULL(func);
return func(command_queue);
}

cl_mem CL_API_CALL clCreateImage2D(cl_context context, cl_mem_flags flags, const cl_image_format *image_format, size_t imageWidth,
size_t imageHeight, size_t image_row_pitch, void *host_ptr, cl_int *errcode_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clCreateImage2D;
WRP_CHECK_NOTNULL(func);
return func(context, flags, image_format, imageWidth, imageHeight, image_row_pitch, host_ptr, errcode_ret);
}

cl_command_queue CL_API_CALL clCreateCommandQueue(cl_context context, cl_device_id device, cl_command_queue_properties properties,
cl_int *errcode_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clCreateCommandQueue;
WRP_CHECK_NOTNULL(func);
return func(context, device, properties, errcode_ret);
}
cl_int CL_API_CALL clEnqueueCopyImage(cl_command_queue queue,
cl_mem src_image,
        cl_mem dst_image,
const size_t * src_origin,
const size_t * dst_origin,
const size_t * region,
        cl_uint        num_events_in_wait_list,
const cl_event * event_wait_list ,
        cl_event *  event) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clEnqueueCopyImage;
WRP_CHECK_NOTNULL(func);
return func(queue, src_image, dst_image, src_origin, dst_origin, region, num_events_in_wait_list, event_wait_list, event);
}

// clCreateCommandQueueWithProperties wrapper
cl_command_queue CL_API_CALL clCreateCommandQueueWithProperties(cl_context context, cl_device_id device, const cl_queue_properties *properties, cl_int *errcode_ret) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clCreateCommandQueueWithProperties;
WRP_CHECK_NOTNULL(func);
return func(context, device, properties, errcode_ret);
}

// clSVMAlloc wrapper, use OpenCLWrapper function.
void* CL_API_CALL clSVMAlloc(cl_context context, cl_mem_flags flags, size_t size, cl_uint align) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clSVMAlloc;
WRP_CHECK_NOTNULL(func);
return func(context, flags, size, align);
}

// clSVMFree wrapper, use OpenCLWrapper function.
void CL_API_CALL clSVMFree(cl_context context, void *buffer) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clSVMFree;
WRP_CHECK_NOTNULL(func);
func(context, buffer);
}

// clEnqueueSVMMap wrapper, use OpenCLWrapper function.
cl_int CL_API_CALL clEnqueueSVMMap(cl_command_queue command_queue, cl_bool blocking, cl_map_flags flags, void *host_ptr,
        size_t size, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clEnqueueSVMMap;
WRP_CHECK_NOTNULL(func);
return func(command_queue, blocking, flags, host_ptr, size, num_events_in_wait_list, event_wait_list, event);
}

// clEnqueueSVMUnmap wrapper, use OpenCLWrapper function.
cl_int CL_API_CALL clEnqueueSVMUnmap(cl_command_queue command_queue, void *host_ptr, cl_uint num_events_in_wait_list,
const cl_event *event_wait_list, cl_event *event) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clEnqueueSVMUnmap;
WRP_CHECK_NOTNULL(func);
return func(command_queue, host_ptr, num_events_in_wait_list, event_wait_list, event);
}

// clSetKernelArgSVMPointer wrapper, use OpenCLWrapper function.
cl_int CL_API_CALL clSetKernelArgSVMPointer(cl_kernel kernel, cl_uint index, const void *host_ptr) {
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clSetKernelArgSVMPointer;
WRP_CHECK_NOTNULL(func);
return func(kernel, index, host_ptr);
}

void * CL_API_CALL clGetExtensionFunctionAddress(const char *func_name){
    auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clGetExtensionFunctionAddress;
    WRP_CHECK_NOTNULL(func);
    return func(func_name);
}

void * CL_API_CALL clGetExtensionFunctionAddressForPlatform(cl_platform_id platform, const char *func_name){
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clGetExtensionFunctionAddressForPlatform;
WRP_CHECK_NOTNULL(func);
return func(platform, func_name);
}

cl_mem CL_API_CALL clImportMemoryARM(cl_context context, cl_mem_flags flags, const cl_import_properties_arm *properties, void *memory, size_t size, cl_int *errcode_ret){
auto func = OpenCLSymbolsOperator::getOpenclSymbolsPtr()->clImportMemoryARM;
WRP_CHECK_NOTNULL(func);
return func(context, flags, properties, memory, size, errcode_ret);
}
